﻿/*
 * 2008 (c) ralfoide at gmail com
 * Project: SharpFlow
 * License: GPL
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace Alfray.SharpFlow.Utils {
    public static class SharpFlowToOfxUtils {

        /// <summary>
        /// Computes the absolute path of a given settings filename.
        /// This prepends the user app data path to the filename, which is composed
        /// of the application name and the version number (without the 4th revision
        /// number.)
        /// </summary>
        /// <param name="filename">The desired leaf filename.</param>
        /// <returns>An absolute path for this file.</returns>
        public static string settingFileName(string filename) {
            // UserAppDataPath uses the version number (f.ex 1.0.42.1234)
            // so let's just keep the major.minor numbers
            string path = System.Windows.Forms.Application.UserAppDataPath;

            try {
                Directory.Delete(path, false /* recursive */);
            } catch (Exception) {
                // ignore
            }

            Regex re = new Regex("^(.*[0-9]+\\.[0-9]+)\\.[0-9]+\\.[0-9]+$");

            Match m = re.Match(path);
            if (m.Success) {
                CaptureCollection c1 = m.Groups[1].Captures;
                if (c1 != null && c1[0] != null)
                    path = c1[0].Value;
            }

            Directory.CreateDirectory(path);

            return Path.Combine(path, filename);
        }
    }
}
